package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.CostRequest;
import com.ruoyi.system.domain.vo.zbg.AnnualCostDataVo;
import com.ruoyi.system.domain.vo.zbg.MonthlyRepairDataVo;
import com.ruoyi.system.domain.vo.zbg.MonthlyRepairTotal;
import com.ruoyi.system.domain.vo.zbg.RepairDateVo;
import com.ruoyi.system.domain.zbg.AnnualCostData;
import com.ruoyi.system.domain.zbg.AnnualPlanData;
import com.ruoyi.system.domain.zbg.MonthlyRepairData;
import com.ruoyi.system.mapper.AnnualCostDataMapper;
import com.ruoyi.system.mapper.CostDataMapper;
import com.ruoyi.system.mapper.MonthlyRepairDataMapper;
import com.ruoyi.system.service.MonthlyRepairDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class MonthlyRepairDataServiceImpl extends ServiceImpl<MonthlyRepairDataMapper, MonthlyRepairData> implements MonthlyRepairDataService {
    @Autowired
    private MonthlyRepairDataMapper monthlyRepairDataMapper;
    @Autowired
    private AnnualCostDataMapper annualCostDataMapper;
    @Autowired
    private CostDataMapper costDataMapper;

    @Transactional(rollbackFor = Exception.class)
    public void saveAll(List<MonthlyRepairData> monthlyRepairDataList) {
//        monthlyRepairDataList.forEach(monthlyRepairDataMapper::insert);
        monthlyRepairDataList.forEach(monthlyRepairData -> {
            monthlyRepairDataMapper.saveOrUpdate(monthlyRepairData);
        });
    }

    public HashMap<String, Object> getStatistics() {
        //查询全市年度维修计划
        //@ TODO 当前年份先写死2024年，后续需要改成动态获取
//        int currentYear = DateUtils.getNowYear();
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        int currentYear = 2024;
        //查询全年维修计划
       AnnualPlanData annualCostData = annualCostDataMapper.selectByYear(currentYear);

        //统计每个月的实际使用费用
       AnnualPlanData annualCostData2= costDataMapper.selectByYear(currentYear);
       annualCostData2.setTotalRepairQuotaSum(annualCostData.getTotalRepairQuotaSum());
       annualCostData2.setElectricQuotaSum(annualCostData.getElectricQuotaSum());
       annualCostData2.setTowerQuotaSum(annualCostData.getTowerQuotaSum());

        // 计算实际维修费占比
        annualCostData2.setRepairUsagePercentage(String.format("%.2f", annualCostData2.getTotalRepairUsageSum() / annualCostData2.getTotalRepairQuotaSum() * 100) + "%");

        // 计算实际电费占比
        annualCostData2.setElectricUsagePercentage(String.format("%.2f", annualCostData2.getElectricUsageSum() / annualCostData2.getElectricQuotaSum() * 100) + "%");

        // 计算实际塔费占比
        annualCostData2.setTowerUsagePercentage(String.format("%.2f", annualCostData2.getTowerUsageSum() / annualCostData2.getTowerQuotaSum() * 100) + "%");
        //同进出全市的费用使用情况
        objectObjectHashMap.put("cityCostUsage",annualCostData2);
        //塔费使用情况\

//       monthlyRepairDataMapper.ins


        return objectObjectHashMap;


    }

    @Override
    public List<RepairDateVo> getCostMaintenanceDetailData(CostRequest costRequest) {
        ArrayList<RepairDateVo> repairDateVos = new ArrayList<>();
        //先判断他查询的是年度还是月度
        if (ObjectUtils.isEmpty(costRequest.getMonth())){
            ArrayList<RepairDateVo> repairDateVos1 = new ArrayList<>();
            //年度查询
            //查询年度的维修费额度
           AnnualCostData annualCostData =  annualCostDataMapper.selectByYearAndDistrict(costRequest.getYear(),costRequest.getDistrict());

           //查询月度的维修费用
            MonthlyRepairTotal monthlyRepairData = monthlyRepairDataMapper.selectByYearAndDistrict(costRequest.getYear(),costRequest.getDistrict());
            if (ObjectUtils.isNotEmpty(monthlyRepairData)) {

                    //线路
                    RepairDateVo repairDateVo = new RepairDateVo();
                    repairDateVo.setSpecialtyName("线路");
                    repairDateVo.setQuota(annualCostData.getRepairLine());
                    repairDateVo.setUsedQuota(monthlyRepairData.getLineTotalUse());
                    //计算线路费用占比
                    //判断计算数据不为空

                    if (ObjectUtils.isNotEmpty(annualCostData.getRepairLine())&&ObjectUtils.isNotEmpty(monthlyRepairData.getLineTotalUse())) {
                        repairDateVo.setProgress(String.format("%.2f", monthlyRepairData.getLineTotalUse() / annualCostData.getRepairLine() * 100) + "%");
                    }
                    repairDateVos1.add(repairDateVo);
                    //家客
                    RepairDateVo repairDateVo2 = new RepairDateVo();
                    repairDateVo2.setSpecialtyName("家客");
                    repairDateVo2.setQuota(annualCostData.getRepairHome());
                    repairDateVo2.setUsedQuota(monthlyRepairData.getCustomerTotalUse());
                    //计算家客费用占比p
                    //判断计算数据不为空
                    if (ObjectUtils.isNotEmpty(annualCostData.getRepairHome())&&ObjectUtils.isNotEmpty(monthlyRepairData.getCustomerTotalUse())) {
                        repairDateVo2.setProgress(String.format("%.2f", monthlyRepairData.getCustomerTotalUse() / annualCostData.getRepairHome() * 100) + "%");
                    }
                    repairDateVos1.add(repairDateVo2);
                    //集客
                    RepairDateVo repairDateVo3 = new RepairDateVo();
                    repairDateVo3.setSpecialtyName("集客");
                    repairDateVo3.setQuota(annualCostData.getRepairGroup());
                    repairDateVo3.setUsedQuota(monthlyRepairData.getCollectTotalUse());
                    //计算集客费用占比
                    //判断计算数据不为空
                    if (ObjectUtils.isNotEmpty(annualCostData.getRepairGroup())&&ObjectUtils.isNotEmpty(monthlyRepairData.getCollectTotalUse())) {
                        repairDateVo3.setProgress(String.format("%.2f", monthlyRepairData.getCollectTotalUse() / annualCostData.getRepairGroup() * 100) + "%");
                    }
                    repairDateVos1.add(repairDateVo3);
                    //动力
                    RepairDateVo repairDateVo4 = new RepairDateVo();
                    repairDateVo4.setSpecialtyName("动力");
                    repairDateVo4.setQuota(annualCostData.getRepairPower());
                    repairDateVo4.setUsedQuota(monthlyRepairData.getPowerTotalUse());
                    //计算动力费用占比
                    repairDateVo4.setProgress(String.format("%.2f", monthlyRepairData.getPowerTotalUse() / annualCostData.getRepairPower() * 100) + "%");
                    repairDateVos1.add(repairDateVo4);
    //                无线
                    RepairDateVo repairDateVo5 = new RepairDateVo();
                    repairDateVo5.setSpecialtyName("无线");
                    repairDateVo5.setQuota(annualCostData.getRepairWireless());
                    repairDateVo5.setUsedQuota(monthlyRepairData.getWirelessTotalUse());
                    //计算无线费用占比
                    //判断计算数据不为空
                    if (ObjectUtils.isNotEmpty(annualCostData.getRepairWireless())&&ObjectUtils.isNotEmpty(monthlyRepairData.getWirelessTotalUse())) {
                        repairDateVo5.setProgress(String.format("%.2f", monthlyRepairData.getWirelessTotalUse() / annualCostData.getRepairWireless() * 100) + "%");
                    }
                    repairDateVos1.add(repairDateVo5);
                    //代维
                    RepairDateVo repairDateVo6 = new RepairDateVo();
                    repairDateVo6.setSpecialtyName("代维");
                    repairDateVo6.setQuota(annualCostData.getRepairMaintenance());
                    repairDateVo6.setUsedQuota(monthlyRepairData.getCoverTotalUse());
                    //计算代维费用占比
                    if (ObjectUtils.isNotEmpty(annualCostData.getRepairMaintenance())&&ObjectUtils.isNotEmpty(monthlyRepairData.getCoverTotalUse())) {
                        repairDateVo6.setProgress(String.format("%.2f", monthlyRepairData.getCoverTotalUse() / annualCostData.getRepairMaintenance() * 100) + "%");
                    }
                    repairDateVos1.add(repairDateVo6);

            }
            repairDateVos.addAll(repairDateVos1);
            return repairDateVos;
        }else if (ObjectUtils.isNotEmpty(costRequest.getMonth())) {
            //月度查询
            //查询月度的维修费用
            MonthlyRepairData monthlyRepairData = monthlyRepairDataMapper.selectByYearAndMonthAndDistrict(costRequest.getYear(),costRequest.getMonth(),costRequest.getDistrict());
            if (ObjectUtils.isNotEmpty(monthlyRepairData)) {
                //线路
                RepairDateVo repairDateVo = new RepairDateVo();
                repairDateVo.setSpecialtyName("线路");
                repairDateVo.setQuota(monthlyRepairData.getLineMonth());
                repairDateVo.setUsedQuota(monthlyRepairData.getLine());
                //计算线路费用占比
                //判断计算数据不为空
                if (ObjectUtils.isNotEmpty(monthlyRepairData.getMaintenance())&&ObjectUtils.isNotEmpty(monthlyRepairData.getId())) {
                    repairDateVo.setProgress(String.format("%.2f", monthlyRepairData.getLine() / monthlyRepairData.getLineMonth() * 100) + "%");
                }
                repairDateVos.add(repairDateVo);
                //家客
                RepairDateVo repairDateVo2 = new RepairDateVo();
                repairDateVo2.setSpecialtyName("家客");
                repairDateVo2.setQuota(monthlyRepairData.getHomeMonth());
                repairDateVo2.setUsedQuota(monthlyRepairData.getHome());
                //计算家客费用占比
                //判断计算数据不为空
                if (ObjectUtils.isNotEmpty(monthlyRepairData.getHome())&&ObjectUtils.isNotEmpty(monthlyRepairData.getHomeMonth())) {
                    repairDateVo2.setProgress(String.format("%.2f", monthlyRepairData.getHome() / monthlyRepairData.getHomeMonth() * 100) + "%");
                }
                repairDateVos.add(repairDateVo2);
                //集客
                RepairDateVo repairDateVo3 = new RepairDateVo();
                repairDateVo3.setSpecialtyName("集客");
                repairDateVo3.setQuota(monthlyRepairData.getJiKeMonth());
                repairDateVo3.setUsedQuota(monthlyRepairData.getJiKe());
                //计算集客费用占比
                //判断计算数据不为空
                if (ObjectUtils.isNotEmpty(monthlyRepairData.getJiKe())&&ObjectUtils.isNotEmpty(monthlyRepairData.getJiKeMonth())) {
                    repairDateVo3.setProgress(String.format("%.2f", monthlyRepairData.getJiKe() / monthlyRepairData.getJiKeMonth() * 100) + "%");
                }
                repairDateVos.add(repairDateVo3);
                //动力
                RepairDateVo repairDateVo4 = new RepairDateVo();
                repairDateVo4.setSpecialtyName("动力");
                repairDateVo4.setQuota(monthlyRepairData.getPower());
                repairDateVo4.setUsedQuota(monthlyRepairData.getPowerMonth());
                //计算动力费用占比
                //判断计算数据不为空
                if (ObjectUtils.isNotEmpty(monthlyRepairData.getPower())&&ObjectUtils.isNotEmpty(monthlyRepairData.getPowerMonth())) {
                    repairDateVo4.setProgress(String.format("%.2f", monthlyRepairData.getPower() / monthlyRepairData.getPowerMonth() * 100) + "%");
                }
                repairDateVos.add(repairDateVo4);
                //无线
                RepairDateVo repairDateVo5 = new RepairDateVo();
                repairDateVo5.setSpecialtyName("无线");
                repairDateVo5.setQuota(monthlyRepairData.getWirelessMonth());
                repairDateVo5.setUsedQuota(monthlyRepairData.getWireless());
                //计算无线费用占比
                //判断计算数据不为空
                if (ObjectUtils.isNotEmpty(monthlyRepairData.getWireless())&&ObjectUtils.isNotEmpty(monthlyRepairData.getWirelessMonth())) {
                    repairDateVo5.setProgress(String.format("%.2f", monthlyRepairData.getWireless() / monthlyRepairData.getWirelessMonth() * 100) + "%");
                }
                repairDateVos.add(repairDateVo5);
                //代维
                RepairDateVo repairDateVo6 = new RepairDateVo();
                repairDateVo6.setSpecialtyName("代维");    //代维
                repairDateVo6.setQuota(monthlyRepairData.getMaintenanceMonth());
                repairDateVo6.setUsedQuota(monthlyRepairData.getMaintenance());
                //计算代维费用占比
                //判断计算数据不为空
                if (ObjectUtils.isNotEmpty(monthlyRepairData.getMaintenance())&&ObjectUtils.isNotEmpty(monthlyRepairData.getMaintenanceMonth())) {
                    repairDateVo6.setProgress(String.format("%.2f", monthlyRepairData.getMaintenance() / monthlyRepairData.getMaintenanceMonth() * 100) + "%");
                }
                repairDateVos.add(repairDateVo6);
            }
            return repairDateVos;
        }

        return new ArrayList<>();
    }
}